package com.am.user.service.impl;


import com.am.common.utils.UserContext;
import com.am.user.domain.Result;
import com.am.user.domain.po.Loginlogs;

import com.am.user.domain.po.User;
import com.am.user.domain.vo.PageVO;
import com.am.user.mapper.LoginlogsMapper;
import com.am.user.mapper.UserMapper;
import com.am.user.service.ILoginlogsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cq
 * @since 2025-02-01
 */
@Service
public class LoginlogsServiceImpl extends ServiceImpl<LoginlogsMapper, Loginlogs> implements ILoginlogsService {
@Autowired
LoginlogsMapper loginlogsMapper;
@Autowired
    UserMapper userMapper;
    /**
     *
     * @param userAngent
     * @param userId
     * @return
     */
    public Result addlogs(String userAngent, String userId) {
        Loginlogs loginlogs = new Loginlogs();
        User user = userMapper.selectById(userId);
        try {
            loginlogs.setBrowserType(getBrowser(userAngent));
            loginlogs.setSystemType(getOS(userAngent));
            loginlogs.setUsername(user.getUsername());
            loginlogs.setUserId(Long.valueOf(userId));
            loginlogsMapper.insert(loginlogs);
            return Result.success();
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 只查询自己的
     * @param pageSize
     * @param pageNum
     * @param userId
     * @return
     */
    public Result<PageVO> pageLogs(int pageSize, int pageNum, String userId) {
        Page<Loginlogs> pageInfo = new Page<>(pageNum,pageSize);
        System.out.println(userId);
        System.out.println(UserContext.getUser());
        //添加排序条件,根据sort来排序
        QueryWrapper<Loginlogs> loginlogsQueryWrapper = new QueryWrapper<Loginlogs>().eq("user_id",userId);
        loginlogsQueryWrapper.orderByDesc("login_time");
        try {

          Page<Loginlogs> page =   loginlogsMapper.selectPage(pageInfo,loginlogsQueryWrapper);
            PageVO<Loginlogs> loginlogsVOPageVO = new PageVO<>();
            loginlogsVOPageVO.setPageNum((int) page.getCurrent());
            loginlogsVOPageVO.setPageSize((int) page.getSize());
            loginlogsVOPageVO.setDatas(page.getRecords());
            loginlogsVOPageVO.setPages((int) page.getPages());
            loginlogsVOPageVO.setTotal((int) page.getTotal());
            return Result.success(loginlogsVOPageVO);
      }catch (Exception e){
          e.printStackTrace();
          return Result.error(e.getMessage());
      }



    }

    private String getBrowser(String userAgent) {
        if (userAgent.contains("Chrome")) {
            return "Chrome";
        } else if (userAgent.contains("Firefox")) {
            return "Firefox";
        } else if (userAgent.contains("Safari")) {
            return "Safari";
        } else if (userAgent.contains("Edge")) {
            return "Edge";
        } else if (userAgent.contains("MSIE") || userAgent.contains("Trident/")) {
            return "Internet Explorer";
        } else {
            return "未知浏览器";
        }
    }
    private String getOS(String userAgent) {
        if (userAgent.contains("Windows NT 10.0")) {
            return "Windows 10";
        } else if (userAgent.contains("Windows NT 11.0")) {
            return "Windows 11";
        } else if (userAgent.contains("Windows NT 6.1")) {
            return "Windows 7";
        } else if (userAgent.contains("Windows NT 6.2")) {
            return "Windows 8";
        } else if (userAgent.contains("Windows NT 6.3")) {
            return "Windows 8.1";
        } else if (userAgent.contains("Mac OS X")) {
            return "macOS";
        } else if (userAgent.contains("Linux")) {
            return "Linux";
        } else if (userAgent.contains("Android")) {
            return "Android";
        } else if (userAgent.contains("iPhone") || userAgent.contains("iPad") || userAgent.contains("iPod")) {
            return "iOS";
        } else {
            return "未知操作系统";
        }
    }
}
